**************************************************************************************************
/*
Creators: Maximilian J. Pany & Raha Imanirad
Course: Gov 2001 / Stat E-200
Date: 5/4/2015
Article:
	Blowing smoke

Replicated Article: 
	Adoption and compliance in second-hand smoking bans: a global econometric analysis
	Richard Perkins, Eric Neumayer
	Int J Public Health (2014) 59:859�866
*/
**************************************************************************************************

**************************************************************************************************
/*
This DO file replicates Perkins & Neumayer (2014) analyses for the year: 

2008.

It is a close copy of their generously provided original DO file; 
a few lines of code were changed here and there to allow porting the results to R for visualization and further analysis.
*/
**************************************************************************************************

version 12.0
drop _all
clear matrix
clear mata
set more off

capture net install outreg2, from(http://fmwww.bc.edu/RePEc/bocode/o)			/* checks whether outreg2 is installed 		*/

**************************************************************************************************
/* Note:
Need to install the -listcoef- command for all parts of this DO file to work properly.
To do so, type -findit spostado- and install the spost9_ado package.
This is NOT necessary to replicate Pany & Imanirad (2015).
*/
**************************************************************************************************

***********************************************************************************
local DIR = "~/Google Drive/gov2001/paper/"  /*change relative path to the directory where the files are located */
cd "`DIR'"
***********************************************************************************

use smoke_2008.dta, replace

***********************************************************************************
* Creating a directory to store the tables created in this DO file *
cap !mkdir tmp
cd tmp
* Creating directory for this year *
cap !mkdir 2008
cd 2008
***********************************************************************************

** Fixing Cote d'Ivoire order to fit with other years **
sort country

** Combining smoke_ban_edu & smoke_ban_univ, as in Perkins & Neumayer (2010) **
replace smoke_ban_edu=1 if smoke_ban_univ == 1
drop smoke_ban_univ

** Combining smoke_ban_restaurant & smoke_ban_pubs_and_bars, as in Perkins & Neumayer (2010) **
gen smoke_ban_rest_or_bars_who = 0
replace smoke_ban_rest_or_bars_who = 1 if smoke_ban_restaurant == 1
replace smoke_ban_rest_or_bars_who = 1 if smoke_ban_pubs_and_bars == 1
drop smoke_ban_restaurant smoke_ban_pubs_and_bars

** Fill missing demographic structure data with data from CIA World Fact Book
replace pop_15_64=70.9 if country=="Palau"
replace pop_65up=6.8 if country=="Palau"
replace pop_15_64=68.7 if country=="Dominica"
replace pop_65up=10.4 if country=="Dominica"
replace pop_15_64=68.4 if country=="St. Kitts and Nevis"
replace pop_65up=7.8 if country=="St. Kitts and Nevis"

** Change the scale of tobacco production variable
replace tobacco_production_filled0=tobacco_production_filled0/1000

** Save processed data for later use in R **
saveold smoke_2008_mod.dta, replace


***********************************************************************************
*** Multiple Imputation ***
capture mi extract 0, clear
mi set wide
mi register imputed cigarette_consumption  health_exp_to_gov_exp ln_gdp_pc
capture mi xtset, clear
capture mi stset, clear
mi impute mvn ln_gdp_pc  cigarette_consumption health_exp_to_gov_exp = smoke_ban_rest_or_bars_who smoke_ban_health smoke_ban_edu smoke_ban_government smoke_ban_offices smoke_ban_pubtransport rule_law_est   tobacco_production_filled0 developed- reg_lac  pop_t- life_exp,  add(100) replace force rseed(2232)

** Save MI data, with MI indicators, for later use in R **
gen mi_ln_gdp_pc = 0
replace mi_ln_gdp_pc = 1 if ln_gdp_pc == . & _1_ln_gdp_pc != .

gen mi_cigarette_consumption = 0
replace mi_cigarette_consumption = 1 if cigarette_consumption == . & _1_cigarette_consumption != .

gen mi_health_exp_to_gov_exp = 0
replace mi_health_exp_to_gov_exp = 1 if health_exp_to_gov_exp == . & _1_health_exp_to_gov_exp != .

saveold smoke_2008_mi.dta, replace
***********************************************************************************


***********************************************************************************
*** TABLE 1 ***
** Smoking ban incidence (existing sample)
* Ban in restaurants OR bars (NOTE: practically no difference to ban in restaurant only or bar only or ban in both)
probit  smoke_ban_rest_or_bars_who  ln_gdp_pc cigarette_consumption tobacco_production_filled0 health_exp_to_gov_exp, robust
* Evaluating model fit *
lroc, nograph  // 64.7%

margins, dydx(*) post
outreg2 using table1.txt, replace excel ci level(95) 2aster  dec(2) sideway

ivprobit  smoke_ban_rest_or_bars_who   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
* Evaluating model fit *
lroc, nograph  // 63.6%

margins, dydx(*) post  predict(pr)
outreg2 using table1.txt, append excel ci level(95) 2aster  dec(2) sideway


** Program for AUC analysis after MI **
cap program drop eroctab
program eroctab, eclass
        version 12.0

        /* Step 1: perform ROC analysis */
        args refvar classvar
        roctab `refvar' `classvar'

        /* Step 2: save estimate and its variance in temporary matrices*/
        tempname b V
        mat `b' = r(area)
        mat `V' = r(se)^2
	local N = r(N)

        /* Step 3: make column names and row names consistent*/
        mat colnames `b' = AUC
        mat colnames `V' = AUC
        mat rownames `V' = AUC

        /*Step 4: post results to e()*/
        ereturn post `b' `V', obs(`N')
        ereturn local cmd "eroctab"
        ereturn local title "ROC area"
end


** Smoking ban incidence (multiply imputed sample)
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
	probit  smoke_ban_rest_or_bars_who  ln_gdp_pc cigarette_consumption    tobacco_production_filled0 health_exp_to_gov_exp, robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
* Evaluating model fit *
mi estimate, cmdok: eroctab smoke_ban_rest_or_bars_who  ln_gdp_pc cigarette_consumption    tobacco_production_filled0 health_exp_to_gov_exp  // 60.3%

capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
	probit  smoke_ban_rest_or_bars_who  ln_gdp_pc cigarette_consumption    tobacco_production_filled0 health_exp_to_gov_exp, robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table1.txt, append excel ci level(95) 2aster  dec(2) sideway


capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
	ivprobit  smoke_ban_rest_or_bars_who   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table1.txt, append excel ci level(95) 2aster  dec(2) sideway
***********************************************************************************


***********************************************************************************
*** TABLE 2 ***
** Ban health facilities
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
  ivprobit  smoke_ban_health ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table2.txt, replace excel ci level(95) 2aster  dec(2) sideway

** Ban education facilities
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
  ivprobit  smoke_ban_edu   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table2.txt, append excel ci level(95) 2aster  dec(2) sideway

** Ban government facilities
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
  ivprobit  smoke_ban_government   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table2.txt, append excel ci level(95) 2aster  dec(2) sideway

** Ban offices
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
  ivprobit  smoke_ban_offices   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table2.txt, append excel ci level(95) 2aster  dec(2) sideway

** Ban public transport
capture program drop mimargins 
program mimargins, eclass properties(mi)
  version 12
  ivprobit  smoke_ban_pubtransport   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp (cigarette_consumption= pop_15_64 pop_65up ) , robust
  margins, dydx(*)  predict(pr) post
end
mi estimate, post cmdok: mimargins 1
outreg2 using table2.txt, append excel ci level(95) 2aster  dec(2) sideway
***********************************************************************************


***********************************************************************************
*** INTERLUDE ***
** Turning smoke_ban_enforcement into indicator variable for purpose of replicating Perkins & Neumayer analyses **
/*
   Note: "m" denotes missing not because no ban, but because info not available even though ban exists.
   We will make use of that distinction in subsequent analyses, but erase it for present purposes.
*/
replace smoke_ban_enforcement = "" if smoke_ban_enforcement == "m"
destring smoke_ban_enforcement, replace
***********************************************************************************


***********************************************************************************
*** TABLE 3 ***
** Smoking ban enforcement (full sample)
ologit  smoke_ban_enforcement cigarette_consumption  ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban, robust
outreg2 using table3.txt, replace excel ci level(95) 2aster  dec(2) sideway
/*
listcoef
listcoef, reverse
listcoef, percent 
listcoef, percent reverse
*/

ivreg2 smoke_ban_enforcement   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban (cigarette_consumption= pop_15_64 pop_65up) , robust first
outreg2 using table3.txt, append excel ci level(95) 2aster  dec(2) sideway

mi estimate, post dots: ologit  smoke_ban_enforcement cigarette_consumption  ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban, robust
outreg2 using table3.txt, append excel ci level(95) 2aster  dec(2) sideway

mi estimate, post dots cmdok: ivreg2 smoke_ban_enforcement   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban (cigarette_consumption= pop_15_64 pop_65up) , robust
outreg2 using table3.txt, append excel ci level(95) 2aster  dec(2) sideway
***********************************************************************************


***********************************************************************************
*** TABLE 4 ***
** Smoking ban enforcement (non-OECD sample)
ologit  smoke_ban_enforcement cigarette_consumption  ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban if inc_high_oecd==0, robust
outreg2 using table4.txt, replace excel ci level(95) 2aster  dec(2) sideway
/*
listcoef
listcoef, reverse
listcoef, percent 
listcoef, percent reverse
*/

ivreg2 smoke_ban_enforcement   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban (cigarette_consumption= pop_15_64 pop_65up)  if inc_high_oecd==0, robust first
outreg2 using table4.txt, append excel ci level(95) 2aster  dec(2) sideway

mi estimate, post dots: ologit  smoke_ban_enforcement cigarette_consumption  ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban if inc_high_oecd==0, robust
outreg2 using table4.txt, append excel ci level(95) 2aster  dec(2) sideway

mi estimate, post dots cmdok: ivreg2 smoke_ban_enforcement   ln_gdp_pc  tobacco_production_filled0 health_exp_to_gov_exp  smoke_ban_number yearsof_partial_rest_ban yearsof_partial_gov_ban yearsof_partial_school_ban yearsof_partial_hospital_ban yearsof_partial_transp_ban (cigarette_consumption= pop_15_64 pop_65up)  if inc_high_oecd==0, robust
outreg2 using table4.txt, append excel ci level(95) 2aster  dec(2) sideway
***********************************************************************************
